package com.cszjzx.member.dao;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.cszjzx.member.po.Member;
public class MemberDAOImpl implements MemberDAO,RowMapper{
    private JdbcHelper helper;
    public MemberDAOImpl() throws IOException {
    	helper=new JdbcHelper();
    }
	public void add(Member m) throws SQLException {
		m.setValue("password", "123456");
		String s1=Member.name[0];   
		String s2=" value(?";
		for(int i=1;i<Member.name.length;i++){
			s1+=","+Member.name[i];
			s2+=",?";
		}
		String sql = "insert into member (" +s1+")" + s2+")";
		Object [] args = m.getValues();
		helper.executeUpdate(sql, args);
	}

	@SuppressWarnings("unused")
	public void addList(List<Member> list) throws SQLException {
		String s1=Member.name[0];           //保存参数列表
		String s2="( ?";                    //对应问号的空位
		for(int i=1;i<Member.name.length;i++){
			s1+=","+Member.name[i];
			s2+=",?";
		}
		s2+=")";
		String sql = "insert into member ("+s1+")"+"values";
		Object []args =new Object[list.size()*Member.name.length];
		int count=0;
		 for(int i=0;i<list.size()-1;i++){
			 sql +=s2+",";
			 Member m=list.get(i);
			 for(int j=0;j<Member.name.length;j++){
				 args[count++]=list.get(i).getValue(Member.name[j]);
			 }
		 }
		 sql +=s2;
		 for(int j=0;j<Member.name.length;j++){
			 args[count++]=list.get(list.size()-1).getValue(Member.name[j]);
		 }
		 helper.executeUpdate(sql, args);
	}
	
	public void delete(String idcard) throws SQLException {
		String sql="delete from member where idcard = ?";
		Object[] args={idcard};
		helper.executeUpdate(sql, args);
		String sql1="delete from templemember where idcard = ?";
		Object[] args2={idcard};
		helper.executeUpdate(sql1, args2);
	}

	@SuppressWarnings("static-access")
	public void update(Member m) throws SQLException {
		int p = Integer.parseInt(m.getValue("process"));
		if(p == 3 || p == 4){
			String[] membercodeS = new String[2];
			try {
				membercodeS = new MembercodeDAOImpl().getStartAndStop();
			} catch (ClassNotFoundException | IOException e) {
				e.printStackTrace();
			}
			int[] temp = new int[100];
			temp[0] = 0;
			for(int i=1;i<=membercodeS[0].length();i++){
				 temp[i] = Integer.parseInt(String.valueOf(membercodeS[0].charAt(i-1)));
			}
			String membercode = membercodeS[0];
			String newMembercode = "";
			m.setValue("membercode", membercode);
			m.setValue("process", "4");
			for(int i = membercode.length(),flag=1;i>=0;i--){
				if(temp[i] + flag == 10){
					temp[i] = 0;
					flag = 1;
				}
				else{
					temp[i] += flag;
					flag = 0;
				}
			}
			if(temp[0] == 0){
				for(int i=1;i<=membercode.length();i++){
					newMembercode += temp[i];
				}
			}
			else{
				for(int i=0;i<=membercode.length();i++){
					newMembercode += temp[i];
				}
			}
			try {
				new MembercodeDAOImpl().update(newMembercode, membercodeS[1]);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String sql="update member set "+Member.name[1]+"=?";
		for(int i=2;i<Member.name.length;i++){
			sql +=" ,"+Member.name[i]+"=? ";
		}
		sql += "where idcard = '" + m.getValue("idcard") + "'";
		Object args[]=new Object[m.name.length-1];
		for(int i=0;i<args.length;i++){
			args[i] = m.getValue(i+1);
		}
		helper.executeUpdate(sql, args);
	}

	@SuppressWarnings("unchecked")
	public List<Member> find(String name, String value) throws ClassNotFoundException, SQLException, IOException {
		String sql="select * from member where "+name+"=?";
		Object[]args={value};
		return  helper.executeQuery(sql, args, this);
	}
	
	@SuppressWarnings("unchecked")
	public List<Member> findByDepartmentNameAndProcess(String departmentName,String process) throws ClassNotFoundException, SQLException, IOException{
		String sql="select * from member where department = ? and process =?";
		Object[]args={departmentName,process};
		return  helper.executeQuery(sql, args, this);
	}

	public List<Member> findDim(String nameInSql, String value)throws ClassNotFoundException, SQLException, IOException {
		return null;
	}
	
	public Object mapRow(ResultSet rs) throws SQLException, ClassNotFoundException, IOException {
	    Member member= new Member();
	    for(int i=0;i<Member.name.length;i++){
	    	member.setValue(i,rs.getString(i+1));
	    }
		return member;
	}
	
	@SuppressWarnings("unchecked")
	public List<Member> findAll() throws ClassNotFoundException, SQLException, IOException{
		String sql="select * from member";
		Object[]args={};
		return  helper.executeQuery(sql, args, this);
	}
	
	public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
	}
	@Override
	public int findProcessByIdcard(String idcard) throws ClassNotFoundException, SQLException, IOException {
		String sql="select process from member where idcard = ?";
		Object[]args={idcard};
		List<Integer> list = helper.executeQuery(sql, args, new RowMapper() {
			@Override
			public Object mapRow(ResultSet rs) throws SQLException, ClassNotFoundException, IOException {
				int p;
				p=Integer.parseInt(rs.getString("process"));
				return p;
			}
		});
		int p;
		if(list.isEmpty()){
			p = 0;
		}
		else if(list.get(0) == -1){
			p = 4;
		}
		else {
			p = list.get(0);
		}
		return p;
	}
}
